////////////////////////////////////////////////////////////////////////////////
//(c) Intelliquip, Inc. 2001
////////////////////////////////////////////////////////////////////////////////
//$Log:
// 6    Version 10.0 (Current View)1.5         2/14/2011 5:16:08 PM   Stephen
//      Kish    IDD#09460: Fixing transportLog warnings
// 5    Version 10.0 (Current View)1.4         11/5/2010 1:05:18 PM   Stephen
//      Kish    IDD#6245: Refactoring changes
// 4    Version 10.0 (Current View)1.3         10/19/2010 2:26:11 PM  Stephen
//      Kish    IDD#6245: Refactoring HttpSessions.get and set Attribute to use
//      DAO classes
// 3    Version 10.0 (Current View)1.2         9/23/2009 5:03:24 PM   Jeff
//      Heigl      IDD#8162: transport log timings added, curve chartable and
//      director cache implemented
// 2    Version 10.0 (Current View)1.1         6/22/2009 12:05:21 PM  Jeff
//      Heigl      IDD#7911: compressor cos page and common xml impl
// 1    Version 10.0 (Current View)1.0         6/19/2009 4:57:13 PM   Jeff
//      Heigl      
//$
//$NoKeywords$
////////////////////////////////////////////////////////////////////////////////
package com.intelliquip.hydraulicselection.control.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.intelliquip.core.TransportLogBean;
import com.intelliquip.core.TransportLogWrapper;
import com.intelliquip.hydraulicselection.xml.AbstractSelectorPage;
import com.intelliquip.util.IEQMimeTypes;
import com.intelliquip.util.ServletUtils;

public class DisplayServlet extends HttpServlet {
	
	public DisplayServlet() {
	}

	/**
	 * Creates a bannerPage based on the bannerID passed in as a parameter.  If bannerID is null, 
	 * retrieves the "bannerIDInSession" from the sessionDAO object.
	 */
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// prevent browser caching of page
		response.setContentType(IEQMimeTypes.getHTMLContentType());
		ServletUtils.setNoCache(response);

		TransportLogWrapper tLog = null;
		try {
			SessionAttributeDAO_Selector dao = new HttpSessionAttributeDAO_Selector(request.getSession());

			PageFactory pageFactory = DisplayController.getPageFactory(request);
			tLog = initTransportLog(pageFactory, dao);
			AbstractSelectorPage page = pageFactory.create(request, tLog);

			SelectorResponseGenerator responseGenerator = new SelectorResponseGenerator(request, response, page);
			responseGenerator.generateResponse(tLog);
			
			response.flushBuffer();
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServletException(e);
		} finally {
			finishTransportLog(tLog);
		}
	}
	
	private static TransportLogWrapper initTransportLog(PageFactory pageFactory, SessionAttributeDAO_Selector dao) {
		if(!pageFactory.isTransportLogSupported()) {
			return null;
		}
		
		TransportLogBean transportLog = dao.getTransportLog();
		String title = pageFactory.getName();
		
		transportLog.setTitle(title);
		transportLog.setServerStartTime(System.currentTimeMillis());
		transportLog.clear();
		
		TransportLogWrapper tLog = new TransportLogWrapper(transportLog, title);
		return tLog;
	}
	
	private static void finishTransportLog(TransportLogWrapper log) {
		if(log != null) {
			TransportLogBean transportLog = log.getTransportLog();
			
			transportLog.setServerEndTime(System.currentTimeMillis());
			
			//for testing mod_deflate compressiont time, before writing output to server
			transportLog.createDeltaEntry("Server to Client Time");
		}
	}


	/**Redirects to doGet()*/
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	

}
